<template>
  <view class="comment-container">
    <view class="header">
      <text class="title">评论</text>
      <view class="close-btn" @click="closePage">关闭</view>
    </view>

    <view class="comment-list">
      <view class="comment-item" v-for="(comment, index) in commentList" :key="index">
        <view class="user-info">
          <image class="avatar" :src="comment.user_avatar || '/static/images/profile.jpg'"></image>
          <text class="username">{{ comment.user_name || '匿名用户' }}</text>
        </view>
        <text class="comment-content">{{ comment.content }}</text>
        <text class="comment-time">{{ formatTime(comment.create_time) }}</text>
      </view>

      <!-- 暂无评论 -->
      <view class="no-comment" v-if="commentList.length === 0">
        暂无评论
      </view>
    </view>

    <view class="comment-input">
      <input class="input" v-model="commentContent" placeholder="写下你的评论..." @confirm="submitComment" />
      <button class="submit-btn" @click="submitComment" :disabled="!commentContent">发送</button>
    </view>
  </view>
</template>

<script>
// 这里可以导入评论相关的API

export default {
  data() {
    return {
      videoId: '',
      commentList: [],
      commentContent: ''
    }
  },
  onLoad(options) {
    // 获取视频ID
    this.videoId = options.videoId || ''
    // 获取评论列表
    this.getCommentList()
  },
  methods: {
    // 关闭页面
    closePage() {
      uni.navigateBack()
    },

    // 获取评论列表
    getCommentList() {
      // 这里可以调用获取评论列表的API
      // 为了演示，这里使用模拟数据
      setTimeout(() => {
        this.commentList = [
          {
            id: 1,
            user_name: '用户1',
            user_avatar: '',
            content: '这个视频太棒了！',
            create_time: Date.now() - 3600000
          },
          {
            id: 2,
            user_name: '用户2',
            user_avatar: '',
            content: '很有帮助，谢谢分享！',
            create_time: Date.now() - 7200000
          }
        ]
      }, 500)
    },

    // 提交评论
    submitComment() {
      if (!this.commentContent.trim()) {
        uni.showToast({
          title: '评论内容不能为空',
          icon: 'none'
        })
        return
      }

      // 这里可以调用提交评论的API
      // 为了演示，这里直接添加到评论列表
      const newComment = {
        id: Date.now(),
        user_name: '当前用户',
        user_avatar: '',
        content: this.commentContent,
        create_time: Date.now()
      }
      this.commentList.unshift(newComment)
      this.commentContent = ''

      uni.showToast({
        title: '评论成功',
        icon: 'success'
      })
    },

    // 格式化时间
    formatTime(time) {
      const date = new Date(time)
      const year = date.getFullYear()
      const month = date.getMonth() + 1
      const day = date.getDate()
      return `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')}`
    }
  }
}
</script>

<style scoped>
.comment-container {
  width: 100%;
  height: 100vh;
  display: flex;
  flex-direction: column;
  background-color: #f5f5f5;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20rpx;
  background-color: #fff;
  border-bottom: 1rpx solid #eee;
}

.title {
  font-size: 36rpx;
  font-weight: bold;
}

.close-btn {
  font-size: 32rpx;
  color: #666;
}

.comment-list {
  flex: 1;
  padding: 20rpx;
  overflow-y: auto;
}

.comment-item {
  margin-bottom: 30rpx;
  padding: 20rpx;
  background-color: #fff;
  border-radius: 10rpx;
}

.user-info {
  display: flex;
  align-items: center;
  margin-bottom: 10rpx;
}

.avatar {
  width: 60rpx;
  height: 60rpx;
  border-radius: 50%;
  margin-right: 10rpx;
}

.username {
  font-size: 28rpx;
  font-weight: bold;
}

.comment-content {
  font-size: 30rpx;
  line-height: 1.4;
  margin-bottom: 10rpx;
}

.comment-time {
  font-size: 24rpx;
  color: #999;
}

.no-comment {
  text-align: center;
  padding: 50rpx 0;
  color: #999;
  font-size: 28rpx;
}

.comment-input {
  display: flex;
  padding: 20rpx;
  background-color: #fff;
  border-top: 1rpx solid #eee;
}

.input {
  flex: 1;
  height: 70rpx;
  padding: 0 20rpx;
  background-color: #f5f5f5;
  border-radius: 35rpx;
  font-size: 28rpx;
}

.submit-btn {
  width: 120rpx;
  height: 70rpx;
  margin-left: 20rpx;
  line-height: 70rpx;
  text-align: center;
  background-color: #007aff;
  color: #fff;
  border-radius: 35rpx;
  font-size: 28rpx;
}

.submit-btn[disabled] {
  background-color: #cccccc;
}
</style>